#include "IO.hh"

namespace IO {
    std::string getString(std::string text_to_print) {
        std::string text;

        std::cout << text_to_print;
        std::cin >> text;
        return text;
    }

    unsigned int getUnsignedInt(unsigned small_limit, unsigned biggest_limit, std::string text_to_print) {
        int num;
        bool get_num = false;

        while (!get_num) {
            std::cout << text_to_print << "\n";
            std::cin >> num;
            if (num < small_limit || num > biggest_limit || std::cin.fail()) {
                std::cin.clear();
                std::cin.ignore(1000, '\n');
                std::cout << "Value should between " << small_limit << " and " << biggest_limit << "\n";
            } else {
                get_num = true;
            }
        }
        
        num = static_cast<unsigned int>(num);
        return num;
    }

    float getFloat(std::string text_to_print) {
        float num;
        bool get_num = false;

        while (!get_num) {
            std::cout << text_to_print;
            std::cin >> num;
            if (num < 0 || std::cin.fail()) {
                std::cin.clear();
                std::cin.ignore(1000, '\n');
                std::cout << "Value should be a float number.";
            } else {
                get_num = true;
            }
        }
        
        return num;
    }

    void clearTerminal() {
        // ANSI 转义码：\033[2J 清屏，\033[1;1H 移动光标到左上角
        std::cout << "\033[2J";
        std::cout.flush(); // 确保立即输出
    }

    void display_teams_massage(std::vector<ICharacter*> hero_team, std::vector<ICharacter*> enemy_team) {
        std::cout << "----------------------------------\n";
        for (int i = hero_team.size() - 1; i >= 0; i--)
        {   
            std::cout << std::to_string(i + 1) << " " << hero_team[i]->toString();
        }
        std::cout << "\n";
        for (int i = 0; i < enemy_team.size(); i++)
        {
            std::cout << std::to_string(i + 1)  << " " << enemy_team[i]->toString();
        }
        std::cout << "----------------------------------\n";
    }

    void display_hero_teams_massage(std::vector<ICharacter*> hero_team) {
        std::cout << "----------------------------------\n";
        for (int i = hero_team.size() - 1; i >= 0; i--)
        {   
            std::cout << std::to_string(i + 1) << " " << hero_team[i]->toString();
        }
        std::cout << "----------------------------------\n";
    }
}